package com.shishuo.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shishuo.cms.constant.GoodsExceptionConstant;
import com.shishuo.cms.dao.UserCallBackGoodsDao;
import com.shishuo.cms.entity.UserCallBackGoods;
import com.shishuo.cms.entity.vo.PageVo;
@Service
public class UserCallBackGoodsService extends BaseService<UserCallBackGoods, Long> {
	private UserCallBackGoodsDao userCallBackGoodsDao;

	@Autowired
	public void setGoodsItemDao(UserCallBackGoodsDao userCallBackGoodsDao) {
		super.setBaseDao(userCallBackGoodsDao);
		this.userCallBackGoodsDao = userCallBackGoodsDao;
	}
	/**
	 * 根据用户编号分页获取收藏的产品
	 * @param pageNum
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public PageVo<UserCallBackGoods> getAllListPage(int pageNum,long userId)
			throws Exception {
		PageVo<UserCallBackGoods> pageVo = new PageVo<UserCallBackGoods>(
				pageNum);
		List<UserCallBackGoods> list = userCallBackGoodsDao.getAllList(
				pageVo.getOffset(), pageVo.getRows(), userId);
		pageVo.setList(list);
		pageVo.setCount(userCallBackGoodsDao.getAllListCount(userId));
		return pageVo;
	}

	public void saveUserCallBackGoods(UserCallBackGoods userCallBackGoods) throws Exception {
		if (null==userCallBackGoods) {
			throw new Exception(GoodsExceptionConstant.SAVE_FAV_GOODS_ERROR_GOODS_IS_NULL);
		}
		if (userCallBackGoodsDao.existUserCallBackGoods(userCallBackGoods.getUserId(), userCallBackGoods.getGoodsId(),userCallBackGoods.getGoodsOrderNum())) {
			throw new Exception(GoodsExceptionConstant.SAVE_FAV_GOODS_ERROR_GOODS_ORDER_HAS_POSTED);
		}
		insert(userCallBackGoods);
	}
}
